package main

import (
	"fmt"
	"google.golang.org/protobuf/proto"
	"log"
	"main/douyin"
	"strconv"
	"time"
)

// 聊天消息
func (w *WebSocketApp) parseChatMsg(payload []byte) {
	Message := &douyin.ChatMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse ChatMessage: %v", err)
		return
	}
	go func() {
		w.saveUser(Message.User)
		w.saveMsg(strconv.FormatUint(Message.Common.MsgId, 10), strconv.FormatUint(Message.User.Id, 10),
			Message.User.NickName, Message.Content, "聊天", strconv.FormatUint(Message.Common.RoomId, 10))
	}()
	log.Printf("【聊天msg】[%d]%s: %s", Message.User.Id, Message.User.NickName, Message.Content)
}

// 礼物消息
func (w *WebSocketApp) parseGiftMsg(payload []byte) {
	Message := &douyin.GiftMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse GiftMessage: %v", err)
		return
	}
	go func() {
		w.saveUser(Message.User)
		w.saveMsg(strconv.FormatUint(Message.Common.MsgId, 10), strconv.FormatUint(Message.User.Id, 10),
			Message.User.NickName, fmt.Sprintf("%s*%d", Message.Gift.Name, Message.ComboCount), "礼物", strconv.FormatUint(Message.Common.RoomId, 10))
	}()
	log.Printf("【礼物msg】%s 送出了 %sx%d", Message.User.NickName, Message.Gift.Name, Message.ComboCount)
}

// 点赞消息
func (w *WebSocketApp) parseLikeMsg(payload []byte) {
	Message := &douyin.LikeMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse LikeMessage: %v", err)
		return
	}
	go func() {
		w.saveUser(Message.User)
		w.saveMsg(strconv.FormatUint(Message.Common.MsgId, 10), strconv.FormatUint(Message.User.Id, 10),
			Message.User.NickName, fmt.Sprintf("点了%d个赞", Message.Count), "点赞",
			strconv.FormatUint(Message.Common.RoomId, 10))
	}()
	log.Printf("【点赞msg】%s 点了%d个赞", Message.User.NickName, Message.Count)
}

// 进入直播间消息
func (w *WebSocketApp) parseMemberMsg(payload []byte) {
	Message := &douyin.MemberMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse LikeMessage: %v", err)
		return
	}
	go func() {
		w.saveUser(Message.User)
		w.saveMsg(strconv.FormatUint(Message.Common.MsgId, 10), strconv.FormatUint(Message.User.Id, 10),
			Message.User.NickName, "进入了直播间", "进入",
			strconv.FormatUint(Message.Common.RoomId, 10))
	}()
	log.Printf("【进场msg】[%d][%s]%s 进入了直播间", Message.User.Id, map[int]string{0: "女", 1: "男"}[int(Message.User.Gender)], Message.User.NickName)
}

// 关注消息
func (w *WebSocketApp) parseSocialMsg(payload []byte) {
	Message := &douyin.SocialMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse LikeMessage: %v", err)
		return
	}
	go func() {
		w.saveUser(Message.User)
		w.saveMsg(strconv.FormatUint(Message.Common.MsgId, 10), strconv.FormatUint(Message.User.Id, 10),
			Message.User.NickName, "关注了主播", "关注",
			strconv.FormatUint(Message.Common.RoomId, 10))
	}()
	log.Printf("【关注msg】[%s]%s 关注了主播", Message.User.NickName, Message.User.NickName)
}

// 直播间统计
func (w *WebSocketApp) parseRoomUserSeqMsg(payload []byte) {
	Message := &douyin.RoomUserSeqMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse LikeMessage: %v", err)
		return
	}
	go func() {
		w.saveMsg(strconv.FormatUint(Message.Common.MsgId, 10), "0",
			"0", strconv.FormatInt(Message.TotalUser, 10), "统计-累计人数",
			strconv.FormatUint(Message.Common.RoomId, 10))
	}()
	log.Printf("【统计msg】当前观看人数: %d, 累计观看人数: %d", Message.Total, Message.TotalUser)
}

// 直播间统计信息
func (w *WebSocketApp) parseRoomStatsMsg(payload []byte) {
	Message := &douyin.RoomStatsMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse LikeMessage: %v", err)
		return
	}
	go func() {
		w.saveMsg(strconv.FormatUint(Message.Common.MsgId, 10), "0",
			"0", strconv.FormatInt(Message.DisplayValue, 10), "统计-当前人数",
			strconv.FormatUint(Message.Common.RoomId, 10))
	}()
	log.Printf("【直播间统计msg】%d 在线观众", Message.DisplayValue)
}

// 直播间排行榜信息
func (w *WebSocketApp) parseRoomRankMsg(payload []byte) {
	Message := &douyin.RoomRankMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse LikeMessage: %v", err)
		return
	}
	currentTime := time.Now()
	formattedTime := currentTime.Format("2006-01-02 15:04:05")
	for i, n := range Message.RanksList {
		go func() {
			w.saveUser(n.User)
			w.saveGiftRank(strconv.FormatUint(Message.Common.MsgId, 10), strconv.FormatUint(n.User.Id, 10),
				n.User.NickName, strconv.Itoa(i), formattedTime, strconv.FormatUint(Message.Common.RoomId, 10))
		}()
		log.Printf("【直播间排行榜msg】%s %d", n.User.NickName, i)
	}

}

// 直播间状态消息
func (w *WebSocketApp) parseControlMsg(payload []byte) {
	Message := &douyin.ControlMessage{} // 替换为实际的 protobuf 结构体
	err := proto.Unmarshal(payload, Message)
	if err != nil {
		log.Printf("Failed to parse LikeMessage: %v", err)
		return
	}
	if Message.Status == 3 {
		log.Println("直播间已关闭")
		w.Conn.Close()
	}
}
